package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.ResourceMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.model.Admin;
import com.cskaoyan.model.AdminExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.AdminInfoVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.InfoDataVO;
import com.cskaoyan.model.vo.LoginDataVO;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: mall38
 * @description:
 * @author: Lexis
 * @create: 2022-05-05 17:37
 **/
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    ResourceMapper resourceMapper;

    @Override
    /**
     * @author: lixiaosheng
     * @description: 根据条件查询所有的管理员
     * @date: 2022/5/5 17:45
     */
    public DataVO<Admin> query(BaseParamBO param, String username) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(sort + " " + order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<Admin> list = adminMapper.selectByExample(adminExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }

    @Transactional
    @Override
    /**
     * @author: lixiaosheng
     * @description: 插入一条admin并获取id
     * @date: 2022/5/5 22:25
     */
    public Admin create(Admin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insertSelective(admin);
        return admin;
    }

    @Transactional
    @Override
    /**
     * @author: lixiaosheng
     * @description: 逻辑删除一个管理员
     * @date: 2022/5/5 22:43
     */
    public void delete(Integer id) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Transactional
    @Override
    /**
     * @author: lixiaosheng
     * @description: 更新管理员的信息
     * @date: 2022/5/6 14:10
     */
    public void update(Admin admin) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andIdEqualTo(admin.getId());
        criteria.andDeletedEqualTo(false);
        admin.setId(null);
        adminMapper.updateByExampleSelective(admin, adminExample);
    }

    @Override
    /**
     * @author: lixiaosheng
     * @description: 根据名字获得用户头像
     * @date: 2022/5/8 17:12
     */
    public LoginDataVO<AdminInfoVO> getAdminInfoByName(String username) {
        LoginDataVO<AdminInfoVO> loginUserDataVO = new LoginDataVO<>();
        AdminInfoVO adminInfoVO = adminMapper.selectAminInfoByName(username);
        loginUserDataVO.setInfo(adminInfoVO);
        return loginUserDataVO;
    }

    @Override
    /**
     * @author: lixiaosheng
     * @description: 得到该用户的权限和角色
     * @date: 2022/5/8 19:01
     */
    public InfoDataVO getAdminPermissionInfoByName(String username) {
        AdminInfoVO adminInfoVO = adminMapper.selectAminInfoByName(username);
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Admin> admin = adminMapper.selectByExample(adminExample);
        List<String> permissions = new ArrayList<>();
        List<String> roles = new ArrayList<>();
        for (Integer roleId : admin.get(0).getRoleIds()) {
            List<String> permissionId = permissionMapper.selectPermissionByRoleId(roleId.toString());
            List<String> permission = new ArrayList<>();
            if (permissionId.contains("*")) {
                permission.add("*");
                return new InfoDataVO(adminInfoVO.getNickName(), adminInfoVO.getAvatar(), roles, permission);
            }
            for (String s : permissionId) {
                String api = resourceMapper.selectApiById(s);
                if (api != null) {
                    permission.add(api);
                }
            }
            String label = roleMapper.selectLabelById(roleId);
            permissions.addAll(permission);
            roles.add(label);
        }
        return new InfoDataVO(adminInfoVO.getNickName(), adminInfoVO.getAvatar(), roles, permissions);
    }
}
